package com.neurosky.thinkgear;

import android.os.Environment;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

public class EkgSenseManager
{
    private TGDevice b;
    public EkgSense ekgSenseObj;
    private float[] c;
    private float[] d;
    private int e;
    private boolean f = true;
    private int g = 0;
    private EkgTemplate[] h;
    public int learnCounter = 0;
    boolean a = true;
    private EkgSenseManager.EkgTimer i;
    public String learnName = "";
    private String j;
    private String k;
    private int l = 0;

    public EkgSenseManager(TGDevice paramTGDevice)
    {
        this.b = paramTGDevice;
        this.ekgSenseObj = new EkgSense(new EkgParameters());
        this.c = new float[this.ekgSenseObj.params.epochLen];
        this.d = new float[this.ekgSenseObj.params.epochLen];
        if ("mounted".equals(Environment.getExternalStorageState()))
        {
            this.j = (Environment.getExternalStorageDirectory() + "/Android/data/com.neurosky.thinkgear/files/EKG/parameters");
            this.k = (Environment.getExternalStorageDirectory() + "/Android/data/com.neurosky.thinkgear/files/EKG/templates");
        }
        else
        {
            this.j = (Environment.getDataDirectory() + "/com.neurosky.thinkgear/files/EKG/parameters");
            this.k = (Environment.getDataDirectory() + "/com.neurosky.thinkgear/files/EKG/templates");
        }
        Log.v("TGDevice", "EKG path: " + this.j);
        if ((!(paramTGDevice = new File(this.k)).exists()) && (paramTGDevice.mkdirs())) {
            Log.d("TGDevice", "Templates directory created successfully");
        }
        loadTemplates();
        this.h = new EkgTemplate[this.ekgSenseObj.params.templateNum];
        this.i = new EkgSenseManager.EkgTimer(this);
        this.i.start();
    }

    public void resetBuffer()
    {
        this.c = new float[this.ekgSenseObj.params.epochLen];
        this.e = 0;
    }

    public void addSample(int paramInt1, int paramInt2)
    {
        if (!TGDevice.ekgPersonalizationEnabled) {
            return;
        }
        if ((paramInt2 == 0) && (this.l == 0))
        {
            this.l = paramInt2;
            return;
        }
        if ((paramInt2 == 0) && (this.l == 200))
        {
            this.ekgSenseObj.currentData = new EkgTemplate();
            resetBuffer();
            this.a = true;
            this.l = paramInt2;
            return;
        }
        if ((paramInt2 == 200) && (this.l == 0))
        {
            this.f = true;
            this.g = 0;
            resetBuffer();
            this.l = paramInt2;
        }
        if ((this.f) && (this.g < 2048))
        {
            this.g += 1;
            return;
        }
        if ((this.f) && (this.g == 2048))
        {
            this.f = false;
            this.g = 0;
            loadTemplates();
        }
        System.arraycopy(this.c, 1, this.d, 0, this.c.length - 1);
        this.d[(this.d.length - 1)] = paramInt1;
        System.arraycopy(this.d, 0, this.c, 0, this.c.length);
        if (this.e < this.c.length) {
            this.e += 1;
        }
        this.l = paramInt2;
    }

    public boolean loadParameterFile(String paramString)
    {
        if (!TGDevice.ekgPersonalizationEnabled) {
            return false;
        }
        Object localObject = new File(this.j);
        paramString = new File(this.j, paramString + ".json");
        try
        {
            if (!((File)localObject).exists())
            {
                Log.d("TGDevice", "EKG folder does not exist.");
                return false;
            }
            if (!paramString.exists())
            {
                Log.d("TGDevice", "Parameter file not found.");
                return false;
            }
            localObject = "";
            paramString = new BufferedReader(new FileReader(paramString));
            do
            {
                localObject = (String)localObject + paramString.readLine();
            } while (paramString.ready());
            paramString.close();
            this.ekgSenseObj.params = new EkgParameters((String)localObject);
            Log.v("TGDevice", "EKG Parmeters loaded");
            return true;
        }
        catch (IOException localIOException)
        {
            Log.v("TGDevice", "WTF: " + localIOException);
        }
        return false;
    }

    public void loadTemplates()
    {
        if (!TGDevice.ekgPersonalizationEnabled) {
            return;
        }
        Object localObject1;
        if ((localObject1 = (localObject1 = new File(this.k)).list()) != null)
        {
            this.ekgSenseObj.reset();
            Object localObject2;
            int m = (localObject2 = localObject1).length;
            for (int n = 0; n < m; n++)
            {
                String str;
                if ((str = localObject2[n]).endsWith(".json")) {
                    try
                    {
                        BufferedReader localBufferedReader = new BufferedReader(new FileReader(new File(this.k, str)));
                        localObject1 = "";
                        EkgTemplate localEkgTemplate = new EkgTemplate();
                        do
                        {
                            localObject1 = (String)localObject1 + localBufferedReader.readLine();
                        } while (localBufferedReader.ready());
                        if (localEkgTemplate.loadJson((String)localObject1))
                        {
                            this.ekgSenseObj.addTemplate(localEkgTemplate);
                            Log.v("TGDevice", "Loaded template: " + str);
                        }
                        else
                        {
                            Log.v("TGDevice", "Template load failed: " + str);
                        }
                    }
                    catch (FileNotFoundException localFileNotFoundException)
                    {
                        (localObject1 = localFileNotFoundException).printStackTrace();
                    }
                    catch (IOException localIOException) {}
                }
            }
        }
    }

    public boolean addUser(String paramString)
    {
        if (!TGDevice.ekgPersonalizationEnabled) {
            return false;
        }
        Object localObject = new File(this.k, paramString + ".json");
        try
        {
            if (!((File)localObject).exists()) {
                ((File)localObject).createNewFile();
            }
            localObject = new FileOutputStream((File)localObject, false);
            this.ekgSenseObj.currentData.subjectName = paramString;
            ((OutputStream)localObject).write(this.ekgSenseObj.currentData.toJSONString().getBytes());
            ((OutputStream)localObject).close();
        }
        catch (FileNotFoundException localFileNotFoundException)
        {
            (localObject = localFileNotFoundException).printStackTrace();
        }
        catch (IOException localIOException)
        {
            return false;
        }
        return true;
    }

    public boolean addUser(EkgTemplate paramEkgTemplate)
    {
        if (!TGDevice.ekgPersonalizationEnabled) {
            return false;
        }
        Object localObject = new File(this.k, paramEkgTemplate.subjectName + ".json");
        try
        {
            if (!((File)localObject).exists()) {
                ((File)localObject).createNewFile();
            }
            (localObject = new FileOutputStream((File)localObject, false)).write(paramEkgTemplate.toJSONString().getBytes());
            ((OutputStream)localObject).close();
        }
        catch (FileNotFoundException localFileNotFoundException)
        {
            (paramEkgTemplate = localFileNotFoundException).printStackTrace();
        }
        catch (IOException localIOException)
        {
            return false;
        }
        return true;
    }

    public boolean saveData()
    {
        if (!TGDevice.ekgPersonalizationEnabled) {
            return false;
        }
        for (int m = 0; m < this.ekgSenseObj.lastTemplateInd; m++)
        {
            Object localObject = new File(this.k, this.ekgSenseObj.templates[m].subjectName + "2.json");
            try
            {
                if (!((File)localObject).exists()) {
                    ((File)localObject).createNewFile();
                }
                (localObject = new FileOutputStream((File)localObject)).write(this.ekgSenseObj.templates[m].toJSONString().getBytes());
                ((OutputStream)localObject).close();
            }
            catch (FileNotFoundException localFileNotFoundException)
            {
                (localObject = localFileNotFoundException).printStackTrace();
            }
            catch (IOException localIOException) {}
        }
        return true;
    }

    public class EkgTimer extends Thread
    {
        private boolean a = true;

        public EkgTimer(EkgSenseManager paramEkgSenseManager) {}

        public void run()
        {
            if (!TGDevice.ekgPersonalizationEnabled) {
                return;
            }
            long l1 = System.currentTimeMillis();
            Random localRandom = new Random();
            int i = 200;
            while (this.a)
            {
                Object localObject;
                if ((System.currentTimeMillis() - l1 > i) && (EkgSenseManager.a(this.b).ekgState != 0) && (EkgSenseManager.b(EkgSenseManager.a(this.b).ekgSenseMan) == EkgSenseManager.a(this.b).ekgSenseMan.ekgSenseObj.params.epochLen))
                {
                    if (EkgSenseManager.a(this.b).ekgSenseMan.ekgSenseObj.processData(EkgSenseManager.c(EkgSenseManager.a(this.b).ekgSenseMan)))
                    {
                        switch (EkgSenseManager.a(this.b).ekgState)
                        {
                            case 1:
                                EkgSenseManager.a(this.b).ekgSenseMan.addUser(EkgSenseManager.a(this.b).ekgSenseMan.learnName);
                                EkgSenseManager.a(this.b).ekgSenseMan.resetBuffer();
                                EkgSenseManager.a(this.b).ekgSenseMan.loadTemplates();
                                EkgSenseManager.a(this.b).ekgState = 0;
                                EkgSenseManager.a(this.b).handler.obtainMessage(269, Integer.valueOf(0)).sendToTarget();
                                Log.v("TGDevice", "Learn complete");
                                break;
                            case 2:
                                if (!EkgSenseManager.a(this.b).ekgSenseMan.a) {
                                    continue;
                                }
                                EkgSenseManager.d(EkgSenseManager.a(this.b).ekgSenseMan)[EkgSenseManager.a(this.b).ekgSenseMan.learnCounter] = EkgSenseManager.a(this.b).ekgSenseMan.ekgSenseObj.currentData;
                                EkgSenseManager.d(EkgSenseManager.a(this.b).ekgSenseMan)[EkgSenseManager.a(this.b).ekgSenseMan.learnCounter].subjectName = EkgSenseManager.a(this.b).ekgSenseMan.learnName;
                                EkgSenseManager.a(this.b).ekgSenseMan.learnCounter += 1;
                                Log.v("TGDevice", "Train step: " + EkgSenseManager.a(this.b).ekgSenseMan.learnCounter);
                                if (EkgSenseManager.a(this.b).ekgSenseMan.learnCounter == EkgSenseManager.a(this.b).ekgSenseMan.ekgSenseObj.params.templateNum)
                                {
                                    new EkgTemplate(EkgSenseManager.a(this.b).ekgSenseMan.learnName);
                                    localObject = null;
                                    try
                                    {
                                        Log.v("TGDevice", "Long learn complete");
                                        (localObject = EkgTemplate.buildStrongTemplate(EkgSenseManager.d(EkgSenseManager.a(this.b).ekgSenseMan))).subjectName = EkgSenseManager.a(this.b).ekgSenseMan.learnName;
                                        EkgSenseManager.a(this.b).ekgSenseMan.addUser((EkgTemplate)localObject);
                                        EkgSenseManager.a(this.b).ekgState = 0;
                                        EkgSenseManager.a(this.b).ekgSenseMan.learnName = "";
                                        EkgSenseManager.a(this.b).ekgSenseMan.loadTemplates();
                                        EkgSenseManager.a(this.b).handler.obtainMessage(269, Integer.valueOf(0)).sendToTarget();
                                        EkgSenseManager.a(this.b).ekgSenseMan.loadTemplates();
                                        this.b.a = false;
                                    }
                                    catch (Exception localException2)
                                    {
                                        Exception localException1;
                                        (localException1 = localException2).printStackTrace();
                                    }
                                    EkgSenseManager.a(this.b).ekgSenseMan.learnCounter = 0;
                                }
                                else
                                {
                                    EkgSenseManager.a(this.b).handler.obtainMessage(270, EkgSenseManager.a(this.b).ekgSenseMan.learnCounter, 0).sendToTarget();
                                }
                                EkgSenseManager.a(this.b).ekgSenseMan.a = false;
                                break;
                            case 3:
                                if (!this.b.a) {
                                    continue;
                                }
                                EkgSenseManager.a(this.b).handler.obtainMessage(268, EkgSenseManager.a(this.b).ekgSenseMan.ekgSenseObj.getClassificationResults()).sendToTarget();
                        }
                    }
                    else
                    {
                        i = 150 + localRandom.nextInt(101);
                        long l2 = System.currentTimeMillis();
                    }
                }
                else {
                    try
                    {
                        Thread.sleep(20L);
                    }
                    catch (InterruptedException localInterruptedException)
                    {
                        (localObject = localInterruptedException).printStackTrace();
                    }
                }
            }
        }

        public void cancel()
        {
            this.a = false;
        }
    }

}
